<template>
    <div class="app-container">
        <div class="warp_padding">
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">账号: </label>
                <el-input v-model="formDate.schId" clearable placeholder="输入账号搜索" style="width: 130px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                
                <span v-if="device!=='mobile'">
                    <label class="labelQuery">名称: </label>
                    <el-input v-model="formDate.schName" clearable placeholder="学校名称搜索" style="width: 130px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                     
                    <label class="labelQuery">学段: </label>
                    <el-select v-model="formDate.schType" clearable placeholder="学段搜索" @change="toQuery()" style="width: 130px;">
                        <el-option
                              v-for="item in phname"
                              :key="item.id"
                              :label="item.phName"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>

                    <label class="labelQuery">学校类型: </label>
                    <el-select v-model="formDate.schLevel" clearable placeholder="学校类型搜索" @change="toQuery()" style="width: 130px;">
                        <el-option
                              v-for="item in schcut"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>

                    <label class="labelQuery">县/区: </label>
                    <area-item @getMessage="showMsg" />
                </span>



                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['sch_all','sch_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                  <eForm ref="form" :is-add="isAdd" :phname="phname" :schcut="schcut"/>
                </div>
            </div>

            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>
                
                <el-table-column prop="schId" label="账号"  sortable :show-overflow-tooltip="true"></el-table-column>           

                <el-table-column prop="schName"  :show-overflow-tooltip="true" label="学校名称" ></el-table-column>

                <el-table-column prop="areaName"  :show-overflow-tooltip="true" label="县/区" ></el-table-column>

                <el-table-column prop="adminid" label="管理员"  :show-overflow-tooltip="true" ></el-table-column> 
                

                <el-table-column prop="schType" label="所属学段" width="80" :formatter="schType">
                </el-table-column>

                <!-- <el-table-column prop="isCock" label="龙头学校" width="80" :formatter="isCock">
                </el-table-column> -->
                
                <el-table-column prop="status" label="状态" width="70">
                     <template slot-scope="scope">
                        <el-tag :type="scope.row.status ? 'info' : 'success'">{{ scope.row.status ? '禁用' : '启用' }}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="schLevel" label="学校类型"  :show-overflow-tooltip="true" :formatter=" schLevel" >
                </el-table-column>

                <!-- <el-table-column prop="schUrl" label="学校域名" :show-overflow-tooltip="true"  width="200"></el-table-column> -->

                <!-- <el-table-column prop="mobile" label="联系电话"   width="120"></el-table-column> -->

                <!-- <el-table-column prop="email" label="邮箱"   width="200"></el-table-column>

                <el-table-column prop="schAddress" label="学校详细地址" :show-overflow-tooltip="true" width="200" ></el-table-column> -->
                
                <!-- <el-table-column prop="createtime" label="创建日期" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.createtime) }}</span>
                    </template>
                </el-table-column> -->

                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button size="mini" type="success" v-if="checkPermission(['sch_all','sch_edit'])" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="checkPermission(['sch_all','sch_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" v-if="checkPermission(['sch_all','sch_del'])" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>
        
    </div>
</template>

<script>


import areaItem from '@/components/area/areaitem'
import checkPermission from '@/utils/permission' 
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { schoolList, schoolDel, phName } from '@/api/request'
import { editTea } from '@/api/data'
import { mapGetters } from 'vuex'
export default {
    components: { eForm, areaItem},
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    data() {
        return {
            phname:[],
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                schId:'',
                schName:'',
                schType:'',
                schLevel:'',
                schArea:''
            },
            isAdd:true,
            schcut: [
                {
                    id: '0',
                    name: '普通学校'
                },
                {
                    id: '1',
                    name: '中心学校'
                },
                {
                    id: '2',
                    name: '教学点'
                },
            ],
         }
    },
    methods: {
        parseTime,
        checkPermission,
        showMsg(tir){
            this.formDate.schArea = tir;
            this.init()
        },
        schLevel(row, column) {
            return row.schLevel == 0 ? '普通学校' : row.schLevel == 1 ? '中心学校' : row.schLevel == 2 ? '教学点': '未知';
        },
        isClasssponsor(row, column) {
            return row.isClasssponsor == 0 ? '是' : row.isClasssponsor == 1 ? '否' : '未知';
        },
        schType(row, column) {
            return row.schType == 1 ? '幼儿园' : row.schType == 2 ? '小学' :  row.schType == 3 ? '初中' :  row.schType == 4 ? '中专' :  row.schType == 5 ? '高中' :  row.schType == 6 ? '大专' :  row.schType == 7 ? '大学' :  row.schType == 8 ? '综合' :'其他';
        },
        isCock(row, column) {
            return row.isCock == 0 ? '是' : row.isCock == 1 ? '否' : '未知';
        },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        add(){
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                schId: '',
                schName:'',
                status: 0,
                mobile:'',
                email:'',
                schAddress:'',
                isCock: '1',
                schType:'',   //学段
                schLevel:'',  //学校类型
                schUrl:'',  
                schArea:'',
                remark:''
            },
            this.$refs.form.dialog = true
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            schoolList(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        
        handleEdit(index, row) {
            this.isAdd = false
            this.$refs.form.dialog = true
            let Resuts = Object.assign({}, row) 
            let isCock = Resuts.isCock.toString()
            let schLevel  = Resuts.schLevel.toString()
            Resuts.isCock = isCock
            Resuts.schLevel = schLevel
            this.$refs.form.form = Resuts
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        schoolDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        schoolDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) {
                                this.formDate.pageNo = this.formDate.pageNo -1
                            }
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        async ready(){
            try {
                let data = await phName()
                this.phname = data.data
            } catch (e) {
                console.log(e)
            }

        },
        
    },
    mounted:function(){
        this.ready()
        this.init()
    },
}

</script>


<style scoped lang="scss">

</style>
